# 导入flask类
from flask import Flask, request
from werkzeug.routing import BaseConverter
import json

app = Flask(__name__)


@app.route("/")
def index():
    return f"<h1>参数传递</h1>"

# https://localhost:5000?s=python&name=张三#test
@app.route("/args")
def args():
    # 方式1 获取单个参数,,推荐使用get方式,不容易出错
    name = request.args.get("name")
    age = request.args["age"]
    # 获取多个参数
    hobby = request.args.getlist("hobby")
    return f"<h1>name--{name}---{age}</h1>"

@app.route("/values")
def values():
    # 方式1
    name = request.values.get("name")
    age = request.values["age"]
    hobby = request.values.getlist("hobby")
    print(name, age, hobby)

    return f"<h1>name--{name}---{age}</h1>"


@app.route("/form",methods = ['POST'])
def form():
    # 获取请求的方法
    print(request.method)
    # 方式1
    name = request.form.get("username")
    age = request.form['password']
    hobby = request.form.getlist("hobby")
    print(name, age, hobby)
    # 将表单数据转换为json格式
    return json.dumps(request.form) 




if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
